package edu.contoller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.annotations.LoginRequired;
import edu.entity.Do.Cart;
import edu.entity.Do.Product;
import edu.entity.Do.ProductImage;
import edu.entity.Vo.CartVo;
import edu.entity.Vo.ProductVo;
import edu.mapper.CartMapper;
import edu.mapper.ProductImageMapper;
import edu.mapper.ProductMapper;
import edu.utils.JwtUtil;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/user")
@CrossOrigin(origins = "*",maxAge = 3600)
public class CartController {

    @Resource
    CartMapper cartMapper;

    @Resource
    ProductMapper productMapper;

    @Resource
    ProductImageMapper productImageMapper;


    @LoginRequired
    @RequestMapping(value = "/addToCart",method = RequestMethod.GET)
    public Integer addToCart(Integer productId, HttpServletRequest request) {
        Integer id = Integer.parseInt(JwtUtil.getClaims(request.getHeader("token")).get("id").asString());


        Cart cart = new Cart();
        cart.setProductId(productId);
        cart.setUserId(id);

        return cartMapper.insert(cart);
    }

    @LoginRequired
    @RequestMapping(value = "/getCartByUserId",method = RequestMethod.GET)
    public List<CartVo> getCartByUserId(HttpServletRequest request) {
        Integer id = Integer.parseInt(JwtUtil.getClaims(request.getHeader("token")).get("id").asString());
        List<Cart> carts = cartMapper.selectList(
                new QueryWrapper<Cart>()
                        .eq("user_id", id)
                .eq("deleted",0)
        );

        List<CartVo> cartVos = new ArrayList<>();

        for (Cart cart : carts) {
            CartVo cartVo = new CartVo();
            cartVo.setId(cart.getId());
            Product product = productMapper.selectById(cart.getProductId());
            ProductVo productVo = new ProductVo();
            BeanUtil.copyProperties(product,productVo);
            ProductImage product_id = productImageMapper.selectOne(
                    new QueryWrapper<ProductImage>()
                            .eq("product_id", product.getId())
            );

            productVo.setPath(product_id.getPath());
            productVo.setCreateTime(DateUtil.format(product.getCreateTime(),"yyyy-MM-dd HH:mm:ss"));
            cartVo.setProductVo(productVo);
            cartVos.add(cartVo);
        }
        return cartVos;
    }

    @LoginRequired
    @RequestMapping(value = "/deleteCartByCartId",method = RequestMethod.GET)
    public Integer deleteCartByCartId(Integer cartId) {
        Cart cart = new Cart();
        cart.setId(cartId);
        cart.setDeleted(1);
        return cartMapper.updateById(cart);
    }
}
